//
//  MVP-P.m
//  DesignPattern
//
//  Created by jun on 2021/4/16.
//

#import "MVP-C.h"
#import "MVP-V.h"
#import "MVP-M.h"
#import "RequestManager.h"
#import "MVP_Presenter.h"

@interface MVP_C ()<MVP_Protocol>

@property MVP_M *model;
@property MVP_V *mvp_v;
@property MVP_Presenter *mvp_p;

@end

@implementation MVP_C

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.mvp_p = [[MVP_Presenter alloc]initWithView:self];
    [self initViews];
    [self getUserData];
}

-(void)initViews{
    self.mvp_v = [[MVP_V alloc] initWithFrame:self.view.frame];
    self.mvp_v.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.mvp_v];
    
    __weak typeof(self) wSelf = self;
    self.mvp_v.touch_block = ^{
        
        __strong typeof(wSelf) sSelf = wSelf;
        if (wSelf.model) {
            if (wSelf.model.title) {
                wSelf.model.title = nil;
                [wSelf updateView];
            }else if(wSelf.model.desc){
                wSelf.model.desc = nil;
                [wSelf updateView];
            }else{
                MVP_M *model = [MVP_M new];
                model.title = @"title";
                model.desc = @"desc";
                wSelf.model = model;
            }
            
        }else{
            MVP_M *model = [MVP_M new];
            model.title = @"title";
            model.desc = @"desc";
            wSelf.model = model;
        }
    };
}

-(void)updateView{
    if (self.model) {
        if (self.model.title) {
            self.mvp_v.backgroundColor = [UIColor greenColor];
        }else if(self.model.desc){
            self.mvp_v.backgroundColor = [UIColor blueColor];
        }else{
            self.mvp_v.backgroundColor = [UIColor redColor];
        }
    }else{
        self.mvp_v.backgroundColor = [UIColor whiteColor];
    }
}


// V=》 P = 〉M
-(void)getUserData{
    [self.mvp_p getData];
}

// M=》 P =protocol =》 V
-(void)onGetDataSuccess:(id)model{
    self.model = [MVP_M new];
    self.model.title = model[@"data"][@"title"];
    self.model.desc = model[@"data"][@"desc"];
    [self updateView];
}

-(void)onGetDataFail:(NSInteger)errorCode des:(NSString *)des{
    self.model = nil;
    [self updateView];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
